﻿using FactoryDeviceSystem.Models;
using FactoryDeviceSystem.Utils;
using Newtonsoft.Json;
using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryDeviceSystem
{
    public partial class PLCSettingForm : Form
    {
        private bool isSaved = true;
        private Dictionary<string, CpuType> cpuTypes = new Dictionary<string, CpuType>();
        private PLCInfo plcInfo;
        public PLCSettingForm()
        {
            InitializeComponent();
        }

        private void PLCSettingForm_Load(object sender, EventArgs e)
        {
            InitCpuTypes();
            LoadPLCInfo();
            InitChangedEvent();
        }

        public event EventHandler<DataEventArgs> OnConfirm;

        private void InitCpuTypes()
        {
            cpuTypes.Clear();
            Type type = typeof(CpuType);
            foreach (var val in Enum.GetValues(type))
            {
                string name = Enum.GetName(type,val);
                cpuTypes.Add(name, (CpuType)val);
            }

            cpuCbBox.DataSource = new BindingSource(cpuTypes, null);
            cpuCbBox.DisplayMember = "Key";
            cpuCbBox.ValueMember = "Value";
        }

        private void LoadPLCInfo()
        {
            plcInfo = ConfigUtil.LoadPlcConfig();

            cpuCbBox.DataBindings.Add("SelectedValue", plcInfo, "CPUType");
            ipTxt.DataBindings.Add("Text", plcInfo, "IP");
            portTxt.DataBindings.Add("Text", plcInfo, "Port");
            rackTxt.DataBindings.Add("Text", plcInfo, "Rack");
            slotTxt.DataBindings.Add("Text", plcInfo, "Slot");
        }

        private void InitChangedEvent()
        {
            cpuCbBox.SelectedIndexChanged += ContentChanged;
            ipTxt.TextChanged += ContentChanged;
            portTxt.TextChanged += ContentChanged;
            rackTxt.TextChanged += ContentChanged;
            slotTxt.TextChanged += ContentChanged;
        }

        private void ContentChanged(object sender, EventArgs e)
        {
            isSaved = false;
        }

        private void confirmBtn_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认保存设置？","提示",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
            if (result != DialogResult.Yes) return;

            SaveToFile();
            OnConfirm?.Invoke(this, new DataEventArgs(plcInfo));
            isSaved = true;
        }

        private void SaveToFile()
        {
            string filePath = Application.StartupPath + "\\ConfigFiles\\Communication.json";
            string data = JsonConvert.SerializeObject(plcInfo);
            File.WriteAllText(filePath, data, Encoding.UTF8);
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            if (!isSaved)
            {
                DialogResult result = MessageBox.Show("当前修改并未保存，是否确认关闭?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }
    }
}
